package test.my.com.myapplicationtest.arrange_view_container;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.LinkedList;
import java.util.List;

/**
 * 排序view容器（排列n列后折行）
 * Created by ah on 2016/3/29.
 */
public class ArrangeViewContainer extends ViewGroup{

    /** 最多容纳view数量 默认为 9 **/
    public static final int VIEW_MAX_NUMBER = 9;
    /** 最大列数 默认为 4 **/
    public static final int VIEW_TOTAL_COLUMN= 4;

    private Context mContext;
    /** 每张图片间距 **/
    private int mSpace;
    /** 每个view宽高 **/
    private int mViewWH;
    /** 此容器摆放view最大列数 **/
    private int mTotalColumn;

    private LinkedList<View> mShowViewList;

    public ArrangeViewContainer(Context context) {
        super(context);
        init(context);
    }

    public ArrangeViewContainer(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ArrangeViewContainer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        this.mContext = context;
        mShowViewList = new LinkedList<>();
        setBackgroundColor(Color.WHITE);
        this.mTotalColumn = VIEW_TOTAL_COLUMN;
    }

    /**
     * 设置一张小图的宽高 和 间距
     * @param vwh 小图宽高
     * @param space 间距
     * @param column 总列数
     */
    public void setVWHandSpace(int vwh,int space,int column){
        this.mViewWH = vwh;
        this.mSpace = space;
        this.mTotalColumn = column;
    }
    /**
     * 设置一张小图的宽高 和 间距
     * @param vwh 小图宽高
     * @param space 间距
     */
    public void setVWHandSpace(int vwh,int space){
        this.mViewWH = vwh;
        this.mSpace = space;
    }

    /**
     * 添加一个view
     * @param view
     */
    public void addViewIn(View view){
        mShowViewList.addFirst(view);
        invalidateContainer();
    }

    /**
     * 添加多个view
     * @param viewList
     */
    public void addViewsIn(List<View> viewList){
        for(View view:viewList){
            mShowViewList.addLast(view);
        }
        invalidateContainer();
    }

    /**
     * 删除指定位置view
     * @param position
     */
    public void removeSpecifyView(int position){
        mShowViewList.remove(position);
        invalidateContainer();
    }

    /**
     * 获取指定位置view
     * @param position
     * @return
     */
    public View getSpecifyView(int position){
        return mShowViewList.get(position);
    }

    /**
     * 刷新container
     */
    private void invalidateContainer(){
        removeAllViews();
        for(int i=0;i<mShowViewList.size();i++){
            // 所要显示的view个数不能超过最大默认数
            if(i == VIEW_MAX_NUMBER){
                break;
            }
            View view = mShowViewList.get(i);
            addView(view);
        }
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

        // 子view总数
        int viewCount = getChildCount();

        // 容器总宽度
        int vgWidth = mTotalColumn*mViewWH+(mTotalColumn - 1)*mSpace;
        // 容器总高度
        int vgHeight = ((viewCount - 1)/mTotalColumn + 1)*mViewWH + (viewCount - 1)/(mTotalColumn - 1)*mSpace;

        for(int i=0;i<viewCount;i++){
            View childView = getChildAt(i);
            ContainerLayoutParams clp = (ContainerLayoutParams) childView.getLayoutParams();
            clp.cLeft = (i % mTotalColumn) * (mViewWH + mSpace);
            clp.cTop = (i / mTotalColumn) * (mViewWH + mSpace);
        }
        setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth
                : vgWidth, (modeHeight == MeasureSpec.EXACTLY) ? sizeHeight
                : vgHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        for(int i=0;i<getChildCount();i++){
            View childView = getChildAt(i);
            ContainerLayoutParams clp = (ContainerLayoutParams) childView.getLayoutParams();
            childView.layout(clp.cLeft,clp.cTop,clp.cLeft+mViewWH,clp.cTop+mViewWH);
        }
    }

    @Override
    public android.view.ViewGroup.LayoutParams generateLayoutParams(
            AttributeSet attrs) {
        return new ContainerLayoutParams(getContext(), attrs);
    }

    @Override
    protected android.view.ViewGroup.LayoutParams generateDefaultLayoutParams() {
        return new ContainerLayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
    }

    @Override
    protected android.view.ViewGroup.LayoutParams generateLayoutParams(
            android.view.ViewGroup.LayoutParams p) {
        return new ContainerLayoutParams(p);
    }

    @Override
    protected boolean checkLayoutParams(android.view.ViewGroup.LayoutParams p) {
        return p instanceof ContainerLayoutParams;
    }


    public static class ContainerLayoutParams extends ViewGroup.LayoutParams{

        public int cLeft =0;
        public int cTop =0;

        public ContainerLayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
        }

        public ContainerLayoutParams(int width, int height) {
            super(width, height);
        }

        public ContainerLayoutParams(LayoutParams source) {
            super(source);
        }
    }
}
